package cn.melonlib.nars.action.comp.service;

import cn.hutool.core.bean.BeanUtil;
import cn.melonlib.cores.commons.comp.service.impl.WorkerBasicService;
import cn.melonlib.cores.identification.comp.messenger.MessengerUtils;
import cn.melonlib.cores.identification.model.entity.Identification;
import cn.melonlib.cores.identification.model.entity.Organization;
import cn.melonlib.cores.jpa.model.bean.collections.JsonMap;
import cn.melonlib.cores.jpa.model.bean.impl.DefaultQuery;
import cn.melonlib.cores.jpa.model.bean.impl.OffsetQueryImpl;
import cn.melonlib.cores.jpa.model.bean.impl.PaginationList;
import cn.melonlib.cores.model.anno.*;
import cn.melonlib.nars.action.model.entity.CollectionNames;
import cn.melonlib.nars.names.comp.service.NamesService;
import cn.melonlib.nars.names.model.entity.MelonNames;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@WorkerComment("收藏资源")
@WorkerService(
        module = "action"
        ,model = @WorkerModel(
                modelType = CollectionNames.class
)
)
public class CollectionNamesService extends WorkerBasicService<CollectionNames> {

    @Resource
    private MessengerUtils messengerUtils;

    @Resource
    private NamesService namesService;

    @WorkerComment("添加到收藏")
    @WorkerQuery
    public CollectionNames addToCollections(@WorkerParameter(value = "path",comment = "路径",required = true) String path)
    throws Exception{
        MelonNames names= namesService.lookup(path);
        Identification identification=getIdentificationMessenger().getIdentification();
        Organization organization=getIdentificationMessenger().getOrganization();
        List<CollectionNames> collectionNamesList=dao.<List<CollectionNames>,DefaultQuery>search(new DefaultQuery(
                CollectionNames.class
                , JsonMap.<JsonMap<Object>>createMap()
                .append("path",JsonMap.createMap().append("eq",path))
                .append("creatorId",JsonMap.createMap().append("eq", BeanUtil.getProperty(identification,"id")))
                .append("creatorCode",JsonMap.createMap().append("eq",identification.getAccount()))
                .append("tenantId",JsonMap.createMap().append("eq",BeanUtil.getProperty(organization,"id")))
                .append("tenantCode",JsonMap.createMap().append("eq",organization.getCode()))
        )).getResult();
        CollectionNames collectionNames=collectionNamesList.isEmpty()?new CollectionNames():collectionNamesList.get(0);
        collectionNames.setPath(path);
        collectionNames.setNamesId(names.getId());
        messengerUtils.setMessengers(collectionNames);
        dao.save(collectionNames);
        return collectionNames;
    }

    @WorkerComment("我的收藏")
    @WorkerQuery
    public PaginationList<CollectionNames> myCollections(
            @WorkerParameter(value = "first",comment = "数量",required = true) Integer first,
            @WorkerParameter(value = "offset",comment = "偏移量",required = true) Long offset){
        Identification identification= getIdentificationMessenger().getIdentification();
        Organization organization=getIdentificationMessenger().getOrganization();
        return dao.<PaginationList<CollectionNames>,OffsetQueryImpl>search(new OffsetQueryImpl(CollectionNames.class,JsonMap.<JsonMap<Object>>createMap()
                .append("creatorId",JsonMap.createMap().append("eq",BeanUtil.getProperty(identification,"id")))
                .append("creatorCode",JsonMap.createMap().append("eq",identification.getAccount()))
                .append("tenantId",JsonMap.createMap().append("eq",BeanUtil.getProperty(organization,"id")))
                .append("tenantId",JsonMap.createMap().append("eq",organization.getCode()))
        ,offset,first)).getResult();
    }
}
